<?php

namespace App\Utils;


use Illuminate\Support\Facades\DB;
use App\Utils\ExcelExportUtils;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/8/12
 * Time: 9:36
 */
class TableDataExcelExportUtils extends ExcelExportUtils
{

    public function exportAllTableDate($table, $file = "")
    {
        $this->setFile($file);
        $sql = "select * from $table";
        $this->exportBySql($sql);
    }

    public function exportBySql($sql, $file = "")
    {
        $this->setFile($file);
        $collection = DB::select($sql);
        if (empty($collection)) {
            return false;
        }
        $first = $collection[0];
        $matrix = json_decode(json_encode($first), true);
        $keys = array_keys($matrix);
        $this->setColumns($keys);
        foreach ($collection as $line) {
            $this->addLineWithHash($line);
        }
        $this->saveToFile();
    }


}